// pages/teach/teach.js
const app = getApp()
const API = require('../../http/API')
const http = require('../../http/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: ["最新", "入门", "进阶", "免费", "图文", "兑换", "专题",],
    // 选中的索引
    actIndex: 0,
    // 搜索框下的图片
    imgUrl: [],
    // 选择的标签
    titleTag: [],
    // 展开或收起
    isOpen: false,
    currentTagID: "000",
    page: 1,
    // 当前筛选的id
    course_subjects_ids: null,
    // 请求完成的数组
    shaixuanData: [],
    // 定义一个变量，判断是搜索或筛选等的请求
    searchType: 0,
    keyword: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      page: 1
    })
    this.getBigPicture()
    this.getSelectTag()
    // 首次获取
    this.getShaiXuan({ type: 1, page: this.data.page, limit: 10, })
    this.search({ type: 1, page: this.data.page, limit: 10 })
  },
  changeNavItem(e) {
    console.log(e);
    this.setData({
      actIndex: e.currentTarget.dataset.index
    })
    if (this.data.actIndex === 0) {
      this.search({ type: 1, page: this.data.page, limit: 10 })
      this.setData({
        searchType: 0
      })
    } else if (this.data.actIndex === 1) {
      this.search({ level: 1, page: this.data.page, limit: 10 })
      this.setData({
        searchType: 3
      })
    } else if (this.data.actIndex === 2) {
      this.search({ level:2, page: this.data.page, limit: 10 })
      this.setData({
        searchType: 4
      })
    } else if (this.data.actIndex === 3) {
      this.search({ is_free:'Y', page: this.data.page, limit: 10 })
      this.setData({
        searchType: 5
      })
    } 
  },

  // 获取搜索下的大图
  getBigPicture() {
    http(API.teachPicture, {
      type: "m",
      position: 3,
      locality: 0,
      order: "weight",
      limit: 6
    }).then(res => {
      console.log(res.data);
      this.setData({
        imgUrl: res.data.data
      })
    })
  },

  // 获取选择的标题
  getSelectTag() {
    http(API.selectTag).then(res => {
      console.log(res.data.course_subjects);
      this.setData({
        titleTag: res.data.course_subjects
      })
    })
  },

  // 首次获取筛选数据
  getShaiXuan(obj) {
    console.log(obj);
    http(API.selectData, obj).then(res => {
      console.log(res.data.data);
      this.setData({
        shaixuanData: res.data.data
      })
    })
  },

  // 是否展开或收起
  changeOpen() {
    this.setData({
      isOpen: !this.data.isOpen
    })
  },
  // 切换标签
  changeTag(e) {
    console.log(e.currentTarget.id);
    this.setData({
      currentTagID: e.currentTarget.id
    })
    if (this.data.course_subjects_ids !== "000") {
      this.search({ type: 1,page: this.data.page,limit: 10,course_subjects_ids: this.data.currentTagID })
      this.setData({
        searchType: 2
      })
    } else {
      this.search({ type: 1, page: this.data.page, limit: 10 })
      this.setData({
        searchType: 0
      })
    }

  },
  // 搜索框搜索
  searchData(e) {

    this.setData({ searchType: 1, keyword: e.detail.value })
    this.search({ type: 1, page: this.data.page, limit: 10, title: e.detail.value })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(111);
    this.setData({
      page: this.data.page + 1
    })
    if (this.data.searchType == 0) {
      this.searchRes({ type: 1, page: this.data.page, limit: 10 })
    } else if (this.data.searchType == 1) {
      this.searchRes({ type: 1, page: this.data.page, limit: 10, title: this.data.keyword })
    } else if (this.data.searchType == 2) {
      this.searchRes({ type: 1, page: this.data.page, limit: 10, course_subjects_ids: this.data.course_subjects_ids })
    } else if (this.data.searchType == 3) {
      this.searchRes({ level: 1, page: this.data.page, limit: 10 })
    }else if (this.data.searchType == 4) {
      this.searchRes({ level:2, page: this.data.page, limit: 10 })
    }else if (this.data.searchType == 5) {
      this.searchRes({ is_free:'Y', page: this.data.page, limit: 10 })
    }
  },
  // 封装一个请求数据方法
  // 参数传递配置对象
  // 只参与触底加载更多不参与其他的搜索请求方法
  searchRes(obj) {
    http(API.selectData, obj).then(res => {
      console.log(res.data);
      this.setData({
        shaixuanData: this.data.shaixuanData.concat(res.data.data)
      })
      console.log(this.data.shaixuanData);
    })
  },
  // 不累加 不参与触底加载更多
  search(obj) {
    http(API.selectData, obj).then(res => {
      console.log(res.data);
      if (res.data.data) {
        this.setData({
          shaixuanData: res.data.data
        })
      }
    })
  },
})